%% 2D Plots for Optimal Contract
model_pick = 'opt';

V = eval(['V_',model_pick]);
Beta = eval(['Beta_',model_pick]);
C = eval(['C_',model_pick]);
K = eval(['K_',model_pick]);

vol_c = eval(['vol_c_',model_pick]);

Suff = eval(['Suff_',model_pick]);

mu_y_paper = PHI_mat.*Eta.^2.*C.^rho/sigma + Y_mat./(1-PHI_mat).*(r - ra + rho/(1-rho).*C.^(1-rho) + 0.5*rho.*Beta.^2);

%% Create variables
epsilon_C = (vol_c./C + Beta)/sigma;

%% Formatting parameters
paperposition = [0 0 8 4.5];
definition = '-r500';
axis_font = 6;
label_font = 10;
legend_font = 8;

edgecolor = 'k';

linewidth = 1;

color1 = 'b';
color2 = 'r';
color3 = [0.4940 0.1840 0.5560];

marker1 = 'o';
marker2 = '^';
marker3 = '*';

marker_size = 5.5;

marker_spacing = [1:20:n_grid];



z1 = 0;
z2 = -0.25;
z3 = -0.5;


z1_lab = char(strcat('$$y$$ = ',{' '}, string(-z1)));
z2_lab = char(strcat('$$y$$ = ',{' '}, string(-z2)));
z3_lab = char(strcat('$$y$$ = ',{' '}, string(-z3)));


y1 = z1.*(1-PHI);
y2 = z2.*(1-PHI);
y3 = z3.*(1-PHI);


%% INTERPOLANTS
V_FUN = griddedInterpolant(Y_mat,PHI_mat,V);

C_FUN = griddedInterpolant(Y_mat,PHI_mat,C);
K_FUN = griddedInterpolant(Y_mat,PHI_mat,K);
Beta_FUN = griddedInterpolant(Y_mat,PHI_mat,Beta);

mu_v_FUN = griddedInterpolant(Y_mat,PHI_mat,mu_v);
mu_y_paper_FUN = griddedInterpolant(Y_mat,PHI_mat,mu_y_paper);

epsilon_K_FUN = griddedInterpolant(Y_mat,PHI_mat,epsilon_K);
epsilon_C_FUN = griddedInterpolant(Y_mat,PHI_mat,epsilon_C);

Suff_FUN = griddedInterpolant(Y_mat,PHI_mat,Suff);

%% V
VAR1 = V_FUN(y1,PHI);
VAR2 = V_FUN(y2,PHI);
VAR3 = V_FUN(y3,PHI);
ymin = min([nanmin(VAR1), nanmin(VAR2), nanmin(VAR3)]);
ymax = max([nanmax(VAR1), nanmax(VAR2), nanmax(VAR3)]);
figure;
hold on
l1 = plot(PHI, VAR1, 'Color', color1, 'LineWidth', linewidth, 'Marker', marker1, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l2 = plot(PHI, VAR2, 'Color', color2, 'LineWidth', linewidth, 'Marker', marker2, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l3 = plot(PHI, VAR3, 'Color', color3, 'LineWidth', linewidth, 'Marker', marker3, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
ylim([ymin ymax])
set(gca,'FontSize',axis_font);
Xla=xlabel('Beliefs ($$\phi$$)','Interpreter','Latex','FontSize',label_font);
set(Xla, 'Units', 'Normalized');
Yla=ylabel('Dual cost function ($$g$$)','Interpreter','Latex','FontSize',label_font);
set(Yla, 'Units', 'Normalized');
set(gcf,'PaperUnits','centimeters')
set(gcf, 'PaperPosition', paperposition);
[hh,icons,plots,txt] = legend([l1 l2 l3],{z1_lab,z2_lab, z3_lab},'Interpreter', 'latex', 'Location','southwest', 'FontSize', legend_font);
icons(4).LineStyle = 'none';
icons(1).Position = [icons(1).Position(1)*1/4*1.2 icons(1).Position(2) icons(1).Position(3)];
icons(5).XData = icons(5).XData/4;
icons(2).Position = [icons(2).Position(1)*1/4*1.2 icons(2).Position(2) icons(2).Position(3)];
icons(6).LineStyle = 'none';
icons(7).XData = icons(7).XData/4;
icons(3).Position = [icons(3).Position(1)*1/4*1.2 icons(3).Position(2) icons(3).Position(3)];
icons(8).LineStyle = 'none';
icons(9).XData = icons(9).XData/4;
legend boxoff
print('-dpng',gcf,['V_',model_pick,'_3line.png'],'-r500');
hold off; clear h; close;


%% Beta
VAR1 = Beta_FUN(y1,PHI);
VAR2 = Beta_FUN(y2,PHI);
VAR3 = Beta_FUN(y3,PHI);
ymin = min([nanmin(VAR1), nanmin(VAR2), nanmin(VAR3)]);
ymax = max([nanmax(VAR1), nanmax(VAR2), nanmax(VAR3)]);
figure;
hold on
l1 = plot(PHI, VAR1, 'Color', color1, 'LineWidth', linewidth, 'Marker', marker1, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l2 = plot(PHI, VAR2, 'Color', color2, 'LineWidth', linewidth, 'Marker', marker2, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l3 = plot(PHI, VAR3, 'Color', color3, 'LineWidth', linewidth, 'Marker', marker3, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
ylim([ymin ymax])
set(gca,'FontSize',axis_font);
Xla=xlabel('Beliefs ($$\phi$$)','Interpreter','Latex','FontSize',label_font);
set(Xla, 'Units', 'Normalized');
Yla=ylabel('Incentives ($$\hat{\beta}$$)','Interpreter','Latex','FontSize',label_font);
set(Yla, 'Units', 'Normalized');
set(gcf,'PaperUnits','centimeters')
set(gcf, 'PaperPosition', paperposition);
[hh,icons,plots,txt] = legend([l1 l2 l3],{z1_lab,z2_lab, z3_lab},'Interpreter', 'latex', 'Location','northwest', 'FontSize', legend_font);
icons(4).LineStyle = 'none';
icons(1).Position = [icons(1).Position(1)*1/4*1.2 icons(1).Position(2) icons(1).Position(3)];
icons(5).XData = icons(5).XData/4;
icons(2).Position = [icons(2).Position(1)*1/4*1.2 icons(2).Position(2) icons(2).Position(3)];
icons(6).LineStyle = 'none';
icons(7).XData = icons(7).XData/4;
icons(3).Position = [icons(3).Position(1)*1/4*1.2 icons(3).Position(2) icons(3).Position(3)];
icons(8).LineStyle = 'none';
icons(9).XData = icons(9).XData/4;
legend boxoff
print('-dpng',gcf,['Beta_',model_pick,'_3line.png'],'-r500');
hold off; clear h; close;


%% C
VAR1 = C_FUN(y1,PHI);
VAR2 = C_FUN(y2,PHI);
VAR3 = C_FUN(y3,PHI);
ymin = min([nanmin(VAR1), nanmin(VAR2), nanmin(VAR3)]);
ymax = max([nanmax(VAR1), nanmax(VAR2), nanmax(VAR3)]);
figure;
hold on
l1 = plot(PHI, VAR1, 'Color', color1, 'LineWidth', linewidth, 'Marker', marker1, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l2 = plot(PHI, VAR2, 'Color', color2, 'LineWidth', linewidth, 'Marker', marker2, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l3 = plot(PHI, VAR3, 'Color', color3, 'LineWidth', linewidth, 'Marker', marker3, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
ylim([ymin ymax])
set(gca,'FontSize',axis_font);
Xla=xlabel('Beliefs ($$\phi$$)','Interpreter','Latex','FontSize',label_font);
set(Xla, 'Units', 'Normalized');
Yla=ylabel('Compensation ratio ($$c$$)','Interpreter','Latex','FontSize',label_font);
set(Yla, 'Units', 'Normalized');
set(gcf,'PaperUnits','centimeters')
set(gcf, 'PaperPosition', paperposition);
[hh,icons,plots,txt] = legend([l1 l2 l3],{z1_lab,z2_lab, z3_lab},'Interpreter', 'latex', 'Location','southwest', 'FontSize', legend_font);
icons(4).LineStyle = 'none';
icons(1).Position = [icons(1).Position(1)*1/4*1.2 icons(1).Position(2) icons(1).Position(3)];
icons(5).XData = icons(5).XData/4;
icons(2).Position = [icons(2).Position(1)*1/4*1.2 icons(2).Position(2) icons(2).Position(3)];
icons(6).LineStyle = 'none';
icons(7).XData = icons(7).XData/4;
icons(3).Position = [icons(3).Position(1)*1/4*1.2 icons(3).Position(2) icons(3).Position(3)];
icons(8).LineStyle = 'none';
icons(9).XData = icons(9).XData/4;
legend boxoff
print('-dpng',gcf,['C_',model_pick,'_3line.png'],'-r500');
hold off; clear h; close;


%% K
VAR1 = K_FUN(y1,PHI);
VAR2 = K_FUN(y2,PHI);
VAR3 = K_FUN(y3,PHI);
ymin = min([nanmin(VAR1), nanmin(VAR2), nanmin(VAR3)]);
ymax = max([nanmax(VAR1), nanmax(VAR2), nanmax(VAR3)]);
figure;
hold on
l1 = plot(PHI, VAR1, 'Color', color1, 'LineWidth', linewidth, 'Marker', marker1, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l2 = plot(PHI, VAR2, 'Color', color2, 'LineWidth', linewidth, 'Marker', marker2, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l3 = plot(PHI, VAR3, 'Color', color3, 'LineWidth', linewidth, 'Marker', marker3, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
ylim([ymin ymax])
set(gca,'FontSize',axis_font);
Xla=xlabel('Beliefs ($$\phi$$)','Interpreter','Latex','FontSize',label_font);
set(Xla, 'Units', 'Normalized');
Yla=ylabel('Capital ratio ($$k$$)','Interpreter','Latex','FontSize',label_font);
set(Yla, 'Units', 'Normalized');
set(gcf,'PaperUnits','centimeters')
set(gcf, 'PaperPosition', paperposition);
[hh,icons,plots,txt] = legend([l1 l2 l3],{z1_lab,z2_lab, z3_lab},'Interpreter', 'latex', 'Location','northwest', 'FontSize', legend_font);
icons(4).LineStyle = 'none';
icons(1).Position = [icons(1).Position(1)*1/4*1.2 icons(1).Position(2) icons(1).Position(3)];
icons(5).XData = icons(5).XData/4;
icons(2).Position = [icons(2).Position(1)*1/4*1.2 icons(2).Position(2) icons(2).Position(3)];
icons(6).LineStyle = 'none';
icons(7).XData = icons(7).XData/4;
icons(3).Position = [icons(3).Position(1)*1/4*1.2 icons(3).Position(2) icons(3).Position(3)];
icons(8).LineStyle = 'none';
icons(9).XData = icons(9).XData/4;
legend boxoff
print('-dpng',gcf,['K_',model_pick,'_3line.png'],'-r500');
hold off; clear h; close;

%% mu_y_paper
VAR1 = mu_y_paper_FUN(y1,PHI);
VAR2 = mu_y_paper_FUN(y2,PHI);
VAR3 = mu_y_paper_FUN(y3,PHI);
ymin = min([nanmin(VAR1), nanmin(VAR2), nanmin(VAR3)]);
ymax = max([nanmax(VAR1), nanmax(VAR2), nanmax(VAR3)]);
figure;
hold on
l1 = plot(PHI, VAR1, 'Color', color1, 'LineWidth', linewidth, 'Marker', marker1, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l2 = plot(PHI, VAR2, 'Color', color2, 'LineWidth', linewidth, 'Marker', marker2, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l3 = plot(PHI, VAR3, 'Color', color3, 'LineWidth', linewidth, 'Marker', marker3, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
ylim([ymin ymax])
set(gca,'FontSize',axis_font);
Xla=xlabel('Beliefs ($$\phi$$)','Interpreter','Latex','FontSize',label_font);
set(Xla, 'Units', 'Normalized');
Yla=ylabel('Drift of $$y$$','Interpreter','Latex','FontSize',label_font);
set(Yla, 'Units', 'Normalized');
set(gcf,'PaperUnits','centimeters')
set(gcf, 'PaperPosition', paperposition);
[hh,icons,plots,txt] = legend([l1 l2 l3],{z1_lab,z2_lab, z3_lab},'Interpreter', 'latex', 'Location','northwest', 'FontSize', legend_font);
icons(4).LineStyle = 'none';
icons(1).Position = [icons(1).Position(1)*1/4*1.2 icons(1).Position(2) icons(1).Position(3)];
icons(5).XData = icons(5).XData/4;
icons(2).Position = [icons(2).Position(1)*1/4*1.2 icons(2).Position(2) icons(2).Position(3)];
icons(6).LineStyle = 'none';
icons(7).XData = icons(7).XData/4;
icons(3).Position = [icons(3).Position(1)*1/4*1.2 icons(3).Position(2) icons(3).Position(3)];
icons(8).LineStyle = 'none';
icons(9).XData = icons(9).XData/4;
legend boxoff
print('-dpng',gcf,['mu_y_',model_pick,'_3line.png'],'-r500');
hold off; clear h; close;

%% VOL log C
VAR1 = epsilon_C_FUN(y1,PHI);
VAR2 = epsilon_C_FUN(y2,PHI);
VAR3 = epsilon_C_FUN(y3,PHI);
ymin = min([nanmin(VAR1), nanmin(VAR2), nanmin(VAR3)]);
ymax = max([nanmax(VAR1), nanmax(VAR2), nanmax(VAR3)]);
figure;
hold on
l1 = plot(PHI, VAR1, 'Color', color1, 'LineWidth', linewidth, 'Marker', marker1, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l2 = plot(PHI, VAR2, 'Color', color2, 'LineWidth', linewidth, 'Marker', marker2, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l3 = plot(PHI, VAR3, 'Color', color3, 'LineWidth', linewidth, 'Marker', marker3, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
ylim([ymin ymax])
set(gca,'FontSize',axis_font);
Xla=xlabel('Beliefs ($$\phi$$)','Interpreter','Latex','FontSize',label_font);
set(Xla, 'Units', 'Normalized');
Yla=ylabel('Pay-perfor. sens. ($$\epsilon_C$$)','Interpreter','Latex','FontSize',label_font);
set(Yla, 'Units', 'Normalized');
set(gcf,'PaperUnits','centimeters')
set(gcf, 'PaperPosition', paperposition);
[hh,icons,plots,txt] = legend([l1 l2 l3],{z1_lab,z2_lab, z3_lab},'Interpreter', 'latex', 'Location','northwest', 'FontSize', legend_font);
icons(4).LineStyle = 'none';
icons(1).Position = [icons(1).Position(1)*1/4*1.2 icons(1).Position(2) icons(1).Position(3)];
icons(5).XData = icons(5).XData/4;
icons(2).Position = [icons(2).Position(1)*1/4*1.2 icons(2).Position(2) icons(2).Position(3)];
icons(6).LineStyle = 'none';
icons(7).XData = icons(7).XData/4;
icons(3).Position = [icons(3).Position(1)*1/4*1.2 icons(3).Position(2) icons(3).Position(3)];
icons(8).LineStyle = 'none';
icons(9).XData = icons(9).XData/4;
legend boxoff
print('-dpng',gcf,['epsilon_C_',model_pick,'_3line.png'],'-r500');
hold off; clear h; close;

%% Suff
VAR1 = Suff_FUN(y1,PHI);
VAR2 = Suff_FUN(y2,PHI);
VAR3 = Suff_FUN(y3,PHI);
ymin = min([nanmin(VAR1), nanmin(VAR2), nanmin(VAR3)]);
ymax = max([nanmax(VAR1), nanmax(VAR2), nanmax(VAR3)]);
figure;
hold on
l1 = plot(PHI, VAR1, 'Color', color1, 'LineWidth', linewidth, 'Marker', marker1, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l2 = plot(PHI, VAR2, 'Color', color2, 'LineWidth', linewidth, 'Marker', marker2, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
l3 = plot(PHI, VAR3, 'Color', color3, 'LineWidth', linewidth, 'Marker', marker3, 'MarkerSize', marker_size, 'MarkerIndices', marker_spacing);
ylim([ymin ymax])
set(gca,'FontSize',axis_font);
Xla=xlabel('Beliefs ($$\phi$$)','Interpreter','Latex','FontSize',label_font);
set(Xla, 'Units', 'Normalized');
Yla=ylabel('$$\frac{\omega - \eta(1-2\phi)\xi}{v^{1-\rho}}$$','Interpreter','Latex','FontSize',label_font);
set(Yla, 'Units', 'Normalized');
set(gcf,'PaperUnits','centimeters')
set(gcf, 'PaperPosition', paperposition);
[hh,icons,plots,txt] = legend([l1 l2 l3],{z1_lab,z2_lab, z3_lab},'Interpreter', 'latex', 'Location','northwest', 'FontSize', legend_font);
icons(4).LineStyle = 'none';
icons(1).Position = [icons(1).Position(1)*1/4*1.2 icons(1).Position(2) icons(1).Position(3)];
icons(5).XData = icons(5).XData/4;
icons(2).Position = [icons(2).Position(1)*1/4*1.2 icons(2).Position(2) icons(2).Position(3)];
icons(6).LineStyle = 'none';
icons(7).XData = icons(7).XData/4;
icons(3).Position = [icons(3).Position(1)*1/4*1.2 icons(3).Position(2) icons(3).Position(3)];
icons(8).LineStyle = 'none';
icons(9).XData = icons(9).XData/4;
legend boxoff
print('-dpng',gcf,['Suff_',model_pick,'_3line.png'],'-r500');
hold off; clear h; close;

